#include <vector>

#include "Inventory.h"
#include "ofMain.h"
#include "Resources.h"

Inventory::Inventory(void) : activeSlot(0) {
    for (int i = 0; i < 10; ++i)
        slots.push_back(new InventorySlot);
}

void Inventory::draw() {
    vector<int> quantities;
    ofPushMatrix();
        ofTranslate(0, ofGetHeight());
        ofScale(32, 32);
        ofPushStyle();
        ofNoFill();
        ofSetColor(0xFFFFFF);
        ofSetLineWidth(2.0f);

        ofPushMatrix();
            ofEnableAlphaBlending();
            ofFill();
            ofSetColor(0, 0, 0, 64);
            for (int i = 0; i < 10; ++i) {
                ofRect(0, -1, 1, 1);
                ofTranslate(1.0f, 0.0f);
            }
            ofNoFill();
            ofDisableAlphaBlending();
        ofPopMatrix();
        ofPushMatrix();
            ofScale(1, -1);
            glEnable(GL_TEXTURE_2D);
            ofSetColor(0xFFFFFF);
            for (int i = 0; i < 10; ++i) {
                quantities.push_back(0);
                int curID;
                if (0 != (curID = slots[i]->getCurrentlyStoredID())) {
                    quantities[i] = slots[i]->getCurrentlyStoredQuantity();
                    glCallList(Resources::get()->listByID(curID));
                }
                ofTranslate(1.0f, 0.0f);
            }
            glDisable(GL_TEXTURE_2D);
        ofPopMatrix();
        ofPushMatrix();
        ofSetColor(0xAAAAAA);
            for (int i = 0; i < 10; ++i) {
                if (i == activeSlot) {
                    ofTranslate(1.0f, 0.0f);
                    continue;
                }
                ofRect(0, -1, 1, 1);
                ofTranslate(1.0f, 0.0f);
            }
        ofPopMatrix();
        ofPushMatrix();
            for (int i = 0; i < 10; ++i) {
                if (i == activeSlot) {
                    ofSetColor(0xFFFFFF);
                    ofRect(0, -1, 1, 1);
                    break;
                }
                ofTranslate(1.0f, 0.0f);
            }
        ofPopMatrix();
        ofPopStyle();
    ofPopMatrix();

    for (int i = 0; i < 10; ++i) {
        char buf[5];
        sprintf(buf, "%i", quantities[i]);
        ofDrawBitmapString(string(buf), i*32+1, ofGetHeight()-3);
    }
}

// Returns true if item was added successfully.
bool Inventory::addItem(int ID, int slot, bool testOnly) {
    // item add logic

    int sz = slots.size();

    if (slot == -1) {
        for (int i = 0; i < sz; ++i) {
            if ((slots[i]->getCurrentlyStoredID() == ID) && (!slots[i]->isFull())) {
                if (!testOnly) slots[i]->add(1);
                return true;
            } else if (slots[i]->getCurrentlyStoredID() == 0) {
                if (!testOnly) slots[i]->add(1, ID);
                return true;
            }
        }
    }

    return false;
}

bool Inventory::removeItem(int slot) {
    if (slot == -1) slot = activeSlot;
    if ((slot < 0) || (slot > 9)) return false;
    if (slots[slot]->getCurrentlyStoredQuantity() <= 0) return false;

    slots[slot]->remove(1);
    return true;
}

bool Inventory::hasRoomForItem(int ID) {
    return addItem(ID, -1, true);
}

bool Inventory::isActiveSlotEmpty() {
    return (slots[activeSlot]->isEmpty());
}

Item* Inventory::getCurrentItem() {
    if (!slots[activeSlot]->isEmpty())
        return new Item(slots[activeSlot]->getCurrentlyStoredID());

    return new Item;
}

int Inventory::getCurrentItemID() {
    return slots[activeSlot]->getCurrentlyStoredID();
}

void Inventory::scroll(bool scrollingDown) {
    int dir;

    if (scrollingDown) dir = 1;
    else dir = -1;

    activeSlot = (activeSlot+dir+10)%10;
}
void Inventory::scrollTo(int slotNumber) {
    activeSlot = slotNumber;
}

Inventory::~Inventory(void) {
    for (int i = 0; i < slots.size(); ++i)
    {
        delete slots[i];
    }
}
